<?php
using("Expression_Operand");

/**
 * Special type of operand which is treated as a value to in the query
 * 
 * <p>A variable is something which is stored in a column in the table.
 * Variables differ from regular operands because they must be represented
 * in a special way.
 * 
 * @author Alexander Schearer <aas11@duke.edu>
 */
class Expression_Operand_Variable extends Expression_Operand {

	public function evaluate() {
		return $this->escape(parent::evaluate());
	}
	
	/**
	 * Escape a given string so that it is safe to use it in a SQL query
	 * 
	 * <p>In addition to strings this method will handle other types of data.  The
	 * data will be treated in a type specific manner so that it plays nice with
	 * MySQL.
	 * 
	 * TODO is it worth refactoring the switch out?
	 * TODO will null ever make it to the default case?
	 *
	 * @param String $data
	 * @return an escaped string safe to use in a MySQL query
	 */
	private function escape($data)
	{
		switch(gettype($data))
		{
			case 'string':
				$data = "'" . mysql_real_escape_string($data) . "'";
				break;
			case 'double':
				$data = str_replace(',', '.', $data);
				break;
			case 'boolean':
				$data = ($data) ? 1 : 0;
				break;
			default:
				if ($data == null)
					$data = "NULL";
		}
		return $data;
	}
	
}
?>